<template>
  <div>
    <van-nav-bar
      title="工单列表"
      left-text="返回"
      left-arrow
      fixed
      @click-left="onClickLeft"
    >
    </van-nav-bar>
    <van-list
      style="margin-top: 50px"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="getList"
    >
      <el-card
        class="box-card"
        v-for="(item, index) in taskList"
        :key="index"
        style="margin: 5px"
      >
        <el-descriptions title="详细信息" :column="1" border>
          <el-descriptions-item label="负责人">{{
            item.createBy
          }}</el-descriptions-item>
          <el-descriptions-item label="工单类型">{{
            item.taskType
          }}</el-descriptions-item>
          <el-descriptions-item label="关联客户">{{
            item.customerName
          }}</el-descriptions-item>
          <el-descriptions-item label="关联商机">{{
            item.orderName
          }}</el-descriptions-item>
          <el-descriptions-item label="开始时间">{{
            parseTime(item.startTime, "{y}-{m}-{d}")
          }}</el-descriptions-item>
          <el-descriptions-item label="结束时间">{{
            parseTime(item.endTime, "{y}-{m}-{d}")
          }}</el-descriptions-item>
          <el-descriptions-item label="实际天数">{{
            item.actualDay
          }}</el-descriptions-item>
          <el-descriptions-item label="紧急程度">{{
            item.taskLevel
          }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <dict-tag
              :options="dict.type.common_status"
              :value="item.status"
            />
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
    </van-list>
  </div>
</template>

<script>
import { listTask, delTask } from "@/api/crm/task";
// import { getTaskDetails } from "@/api/crm/dataStatistics";
export default {
  dicts: [
    "task_level",
    "task_type",
    "task_mode",
    "common_status"
  ],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskNo: null,
        taskType: null,
        taskMode: null,
        phone: null,
        address: null,
        startTime: null,
        endTime: null,
        actualDay: null,
        taskLevel: null,
        coPerson: null,
        taskPrice: null,
        files: null,
        illustrate: null,
        receivablePayable: null,
        receivablePaid: null,
        type: 1,
      },
      dateRange: [],
      loading: false,
      total: 0,
      taskList: [],
      finished: false
    };
  },
  methods: {
     /** 查询工单列表 */
    async getList() {
      
      this.loading = true;
      try {
        let res = null;
        if(this.$route.query.type === '1'){
          this.queryParams.orderId = this.$route.query.orderId
          this.queryParams.createBy = this.$route.query.createBy
          res = await listTask(this.addDateRange(this.queryParams, this.dateRange));
        } else {
          this.queryParams = this.$route.query.queryParams
          res = await getTaskDetails(this.addDateRange(this.queryParams, this.dateRange))
        }
        
        // 判断是否加载完所有数据
        if (this.taskList.length >= res.total) {
          this.finished = true;
        } else {
          this.taskList.push(...res.rows);
          this.queryParams.pageNum += 1;
        }
      } catch (error) {
        console.error("加载数据失败", error);
      } finally {
        this.loading = false;
      }
    },

    
    onClickLeft() {
      this.$router.back();
    },
  },
  created() {
    this.getList()
  },
};
</script>
